package com.changgou.system.goods.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.changgou.goods.pojo.Brand;
import com.changgou.system.goods.dao.BrandMapper;
import com.changgou.system.goods.service.BrandService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    /**
     * 品牌列表查询
     *
     * @return
     */
    @Override
    public List<Brand> findList() {
        return this.list();
    }

    /**
     * 根据id查询品牌信息
     *
     * @param id
     * @return
     */
    @Override
    public Brand findById(Integer id) {
        return this.getById(id);
    }

    /**
     * 品牌新增
     *
     * @param brand
     */
    @Override
    @Transactional
    public void add(Brand brand) {
        this.save(brand);
    }

    /**
     * 品牌修改
     *
     * @param brand
     */
    @Override
    @Transactional
    public void update(Brand brand) {
        this.updateById(brand);
    }

    /**
     * 根据id删除品牌信息
     *
     * @param id
     */
    @Override
    @Transactional
    public void delById(Integer id) {
        this.removeById(id);
    }

    /**
     * 品牌列表条件查询
     *
     * @param searchMap
     * @return
     */
    @Override
    public List<Brand> list(Map<String, Object> searchMap) {
        return this.list(Wrappers.<Brand>lambdaQuery()
                .like(ObjectUtil.isNotEmpty(searchMap.get("name")), Brand::getName, searchMap.get("name"))
                .eq(ObjectUtil.isNotEmpty(searchMap.get("letter")), Brand::getLetter, searchMap.get("letter")));
    }

    @Override
    public Page<Brand> findPage(int page, int size) {
        PageHelper.startPage(page, size);
        return (Page<Brand>) this.list();
    }

    @Override
    public Page<Brand> findPage(Map<String, Object> searchMap, int page, int size) {
        //设置分页
        PageHelper.startPage(page, size);
        return (Page<Brand>) this.list(searchMap);
    }

    @Override
    public List<Brand> findByCategoryName(String name) {
        return brandMapper.findByCategoryName(name);
    }
}
